/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.ecommerce.dao.impl;

import br.com.ecommerce.dao.VoucherPromocaoClienteDAO;
import br.com.ecommerce.dao.pojo.EntityPojo;
import br.com.ecommerce.dao.pojo.VoucherPromocaoCliente;
import br.com.ecommerce.exceptions.DatabaseException;
import br.com.ecommerce.exceptions.IdInvalidoException;
import br.com.ecommerce.exceptions.ObjetoInvalidoException;
import br.com.ecommerce.factory.DatabaseFactory;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Classe que implementa os métodos que manipulam a entidade VoucherPromocaoCliente
 * @author Afonso
 */
public class VoucherPromocaoClienteDAOImpl implements VoucherPromocaoClienteDAO {

    @Override
    public void create(EntityPojo obj) throws ObjetoInvalidoException,
                                                DatabaseException {
        if (obj == null || !(obj instanceof VoucherPromocaoCliente)) {

           throw new ObjetoInvalidoException("Objeto POJO é null ou não "
                   + "é do tipo VoucherPromocaoCliente");
        }

        VoucherPromocaoCliente voucherPromo = (VoucherPromocaoCliente)obj;
        Connection conn = DatabaseFactory.getConnection();
        Statement stmt;
        String sqlInsert;
        try {

            stmt = conn.createStatement();
            sqlInsert = "insert into VOUCHER_PROMOCAO_CLIENTE"
                    + " values (VOUCHER_PROMOCAO_CLIENTE_SEQ.nextval, "
                    + "'" + voucherPromo.getIdPromocao() + "', "
                    + "'" + voucherPromo.getIdCliente() + "', "
                    + "TO_DATE('" + voucherPromo.getValidade() + "', 'dd/mm/YYYY'))";

            stmt.execute(sqlInsert);
            stmt.close();
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.SEVERE, ex.getMessage(), ex);

            throw new DatabaseException(ex.getMessage(), ex.getCause());
        }
    }

    @Override
    public EntityPojo readById(Integer idPrimaryKey) throws IdInvalidoException,
                                                            DatabaseException {
        if (idPrimaryKey == null) {
           throw new IdInvalidoException("ID inválido");

        }

        VoucherPromocaoCliente voucherPromo = new VoucherPromocaoCliente();
        Connection conn = DatabaseFactory.getConnection();
        Statement stmt;
        String sql;
        try {
            sql = "SELECT * FROM VOUCHER_PROMOCAO_CLIENTE "
                    + "WHERE ID_VOUCHER="+idPrimaryKey;

            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.INFO, "String SQL : " + sql);

            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            if (rs.next()) {
                voucherPromo.setIdPrimaryKey(idPrimaryKey);
                voucherPromo.setIdPromocao(rs.getInt(2));
                voucherPromo.setIdCliente(rs.getInt(3));
                voucherPromo.setValidade(rs.getString(4));

            } else {
                 throw new DatabaseException("Não há dados na base de dados para"
                         + " a query: " + sql);

            }
            stmt.close();
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.SEVERE, ex.getMessage(), ex);

            throw new DatabaseException(ex.getMessage(), ex.getCause());
        }

        return voucherPromo;
    }

    @Override
    public List readAll() throws DatabaseException {
        Connection conn = DatabaseFactory.getConnection();
        Statement stmt;
        String sql;
        List<VoucherPromocaoCliente> list = new ArrayList<VoucherPromocaoCliente>();
        try {
            sql = "SELECT * FROM VOUCHER_PROMOCAO_CLIENTE ORDER BY ID_VOUCHER";

            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.INFO, "String SQL : " + sql);

            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                VoucherPromocaoCliente voucherPromo = new VoucherPromocaoCliente();
                voucherPromo.setIdPrimaryKey(rs.getInt(1));
                voucherPromo.setIdPromocao(rs.getInt(2));
                voucherPromo.setIdCliente(rs.getInt(3));
                voucherPromo.setValidade(rs.getString(4));

//                Logger.getLogger(ClienteDAOImpl.class.getName())
//                            .log(Level.INFO, "String SQL : " + cliente.toString());

                list.add(voucherPromo);
            }


            stmt.close();
            conn.close();

        } catch (SQLException ex) {
            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.SEVERE, ex.getMessage(), ex);

            throw new DatabaseException(ex.getMessage(), ex.getCause());
        }

        return list;

    }

    @Override
    public void deleteById(Integer idPrimaryKey) throws IdInvalidoException,
                                                        DatabaseException {
        if (idPrimaryKey == null) {
           throw new IdInvalidoException("ID inválido");

        }

        Connection conn = DatabaseFactory.getConnection();
        Statement stmt;
        String sql;

        try {
            sql = "DELETE FROM VOUCHER_PROMOCAO_CLIENTE "
                    + "WHERE ID_VOUCHER='" + idPrimaryKey+"'";

            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.INFO, "String SQL : " + sql);

            stmt = conn.createStatement();

            stmt.execute(sql);

            stmt.close();
            conn.close();

        } catch (SQLException ex) {
            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.SEVERE, ex.getMessage(), ex);

            throw new DatabaseException(ex.getMessage(), ex.getCause());
        }
    }

    @Override
    public void update(EntityPojo obj) throws ObjetoInvalidoException,
                                                DatabaseException {
        if (obj == null || !(obj instanceof VoucherPromocaoCliente)) {
           throw new ObjetoInvalidoException("Objeto POJO é null ou não "
                   + "é do tipo VoucherPromocaoCliente");
        }

        VoucherPromocaoCliente voucherPromo = (VoucherPromocaoCliente)obj;
        Connection conn = DatabaseFactory.getConnection();
        Statement stmt;
        String sql;

        try {
            sql = "UPDATE VOUCHER_PROMOCAO_CLIENTE SET ID_PROMOCAO='" +
                    voucherPromo.getIdPromocao() + "', "
                    + "ID_CLIENTE='" + voucherPromo.getIdCliente() + "', "
                    + "VALIDADE='" + voucherPromo.getValidade() + "' "
                    + "WHERE ID_VOUCHER = " + voucherPromo.getIdPrimaryKey();

            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.INFO, "String SQL : " + sql);

            stmt = conn.createStatement();
            stmt.execute(sql);

            stmt.close();
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(VoucherPromocaoClienteDAOImpl.class.getName())
                            .log(Level.SEVERE, ex.getMessage(), ex);

            throw new DatabaseException(ex.getMessage(), ex.getCause());
        }

    }
}
